<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');

/**
 * KMS_Video_Segment Controller
 */
class KMS_VideoControllerKMS_Video_Segment extends JControllerForm
{
	
	public function save()
	{
		$app		=	JFactory::getApplication();
		$jformarr = $app->input->get('jform', '', 'array');
		$label_value = $jformarr['label_value'];
		
		$label_id = $this->addLabel($label_value);
		
		$video_id = $jformarr['video_id'];
		$segment_id = $jformarr['segment_id'];
		$time_frame = $jformarr['time_frame'];
		//If the segment_id is missing, we need to create one...
		if(empty($segment_id) || $segment_id == 0)
		{
			$this->addSegment($time_frame, $video_id);
			$msg		=	JText::_( 'New Segment Added!!! [' . $time_frame .']');
			$msgType	=	'message';
			$this->setRedirect( 'index.php?option=com_kms_video&view=kms_video&video_id=' . $video_id, $msg, $msgType);
			return 0;
		}
		//$app->enqueueMessage('label_value = ' . $label_value . ', label_id = '. $label_id . ', video_id = '. $video_id . ', segment_id = '. $segment_id);
		$this->linkLabelToVideo($label_id, $segment_id);
		
		$msg		=	JText::_( 'COM_KMS_VIDEO_KMS_VIDEO_SEGMENT_SAVED' );
		$msgType	=	'message';
		$this->setRedirect( $this->_getReturnPage(), $msg, $msgType );
	}
	
	public function add()
    {
                $app            =       JFactory::getApplication();
               
                $video_id = $app->input->getInt('video_id');
       
                $this->setRedirect('index.php?option=com_kms_video&view=kms_video_segment&layout=edit&video_id='.$video_id);
    }

	public function cancel()
	{
		$app		=	JFactory::getApplication();
		$video_id = $app->input->getInt('video_id');
		parent::cancel();
		$this->setRedirect( 'index.php?option=com_kms_video&view=kms_video&video_id=' . $video_id);
		
	}
	
	
	public function linkLabelToVideo($label_id, $segment_id)
        {
				
				$db = JFactory::getDBO();
                $query = $db->getQuery(true);
                $query = 'insert into #__kms_labels_segments (label_id, segment_id) values ('. $label_id . ', ' . $segment_id . ')';
                $db->setQuery((string)$query);
                $r = $db->loadObjectList();
                
                return $r;
		}
		
		public function labelExists($label_value)
		{		$app		=	JFactory::getApplication();
				$db = JFactory::getDBO();
                $query = $db->getQuery(true);
                $query = 'select label_id from #__kms_labels where UPPER(label_value) = ' . strtoupper($label_value);
                $db->setQuery((string)$query);
                $r = $db->loadObjectList();
                
                
                if (empty($r)){
					//$app->enqueueMessage('Label dont exist!!' . empty($r));
					return false;
				}
                return true;
		}
		
		private function addLabel($label_value)
		{	$app		=	JFactory::getApplication();
			
			
			if(!$this->labelExists($label_value)){
				$db = JFactory::getDBO();
				$query = $db->getQuery(true);
                $query = 'insert into #__kms_labels (label_value) values ("'.$label_value .'")';
                $db->setQuery((string)$query);
                $r = $db->loadObjectList();
                //$app->enqueueMessage('Creating label with value: ' . $label_value . '. Empty? ' . empty($r) . ' Query = ' . (string)$query);
			}
			$db = JFactory::getDBO();
			$query = $db->getQuery(true);
			$query = 'select label_id from #__kms_labels where label_value = "' . $label_value . '"';
			$db->setQuery((string)$query);
			$r = $db->loadObjectList();
			return $r[0]->label_id;
			
		}
		
		private function addSegment($segment_value, $video_id)
		{
			
			$db = JFactory::getDBO();
			$query = $db->getQuery(true);
            $query = 'insert into #__kms_segments (video_id, time_frame) values ('.$video_id .',"'.$segment_value .'")';
            $db->setQuery((string)$query);
            $r = $db->loadObjectList();
             
		}
		
		// _getReturnPage
		protected function _getReturnPage( $base = false )
		{
			$app	=	JFactory::getApplication();
			$return	=	$app->input->getBase64( 'return' );
			
			if ( empty( $return ) || !JUri::isInternal( base64_decode( $return ) ) ) {
				return ( $base == true ) ? JURI::base() : 'index.php?option=com_kms_video';
			} else {
				return base64_decode( $return );
			}
		}
}
